﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;

namespace GameTank
{
    public class StartMenu : Form
    {
        protected TextMenuComponent menu;
        protected readonly Texture2D elements;
        // Audio
        protected AudioLibrary audio;
        // Spritebatch
        protected SpriteBatch spriteBatch = null;

        public StartMenu(SpriteFont smallFont, SpriteFont largeFont, Texture2D background)
            : base()
        {

            Childs.Add(new ImageComponent(background, ImageComponent.DrawMode.Stretch));
            audio = Game1.AudioLib;
            // Create the Menu
            string[] items = { "New campaign", "Continue campaign", "Help", "Quit" };
            menu = new TextMenuComponent(smallFont, largeFont);
            menu.SetMenuItems(items);
            Childs.Add(menu);
        }

        /// <summary>
        /// Show the start scene
        /// </summary>
        public override void Show()
        {
            //audio.NewMeteor.Play();

            // Put the menu centered in screen
            menu.Position = new Vector2((Game1.windowsBounder.Width -
                                          menu.Width) / 2, Game1.windowsBounder.Height -
                                          menu.Height - 10);

            // These elements will be visible when the 'Rock Rain' title
            // is done.
            this.Enable = true;
            menu.Visible = true;
            menu.Enable = true;
            
            MediaPlayer.Play(audio.StartMusic);
            base.Show();
        }

        /// <summary>
        /// Hide the start scene
        /// </summary>
        public override void Hide()
        {
            MediaPlayer.Stop();
            menu.Visible = false;
            menu.Enable = false;
            base.Hide();
        }

        /// <summary>
        /// Gets the selected menu option
        /// </summary>
        public int SelectedMenuIndex
        {
            get { return menu.SelectedIndex; }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            this.Handle();
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }

        public void Handle()
        {
            MyGamePad gamepad = Game1.gamepad;

            if (gamepad.Select.IsRelease && gamepad.Select.IsDownBefore)
            {
                int idx = menu.SelectedIndex;

                switch (idx)
                {
                    case 0:
                        this.Hide();
                        FormContent.gamePlay.LoadNewGame(Game1.Content1,MapDTO.Load("map1.xml"));
                        FormContent.gamePlay.Show();
                        break;
                    case 1:
                        // continue campain
                        break;
                    case 2:
                        //help
                        break;
                    case 3:
                        Game1.exit = true;
                        break;
                    default:
                        break;
                }
            }
            else
            {
            }
        }
    }
}
